package com.bright.ghj.overt.manager.impl;

import com.bright.ghj.common.util.StringUtil;
import com.bright.ghj.overt.converter.NavigateConverter;
import com.bright.ghj.overt.converter.RightRoleConverter;
import com.bright.ghj.overt.converter.UserConverter;
import com.bright.ghj.overt.manager.UserManager;
import com.bright.ghj.overt.pojo.dto.RightRoleDTO;
import com.bright.ghj.overt.pojo.dto.UserDTO;
import com.bright.ghj.overt.pojo.po.primary.Navigate;
import com.bright.ghj.overt.pojo.po.primary.RightRole;
import com.bright.ghj.overt.pojo.po.primary.User;
import com.bright.ghj.overt.repository.primary.NavigateRepository;
import com.bright.ghj.overt.repository.primary.RightRoleRepository;
import com.bright.ghj.overt.repository.primary.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * @Author hxj
 * @Date 2024/1/23 09:13
 * @Description
 */
@Component
@RequiredArgsConstructor
public class UserManagerImpl implements UserManager {

    private final UserRepository userRepository;
    private final RightRoleRepository rightRoleRepository;
    private final NavigateRepository navigateRepository;
    private final UserConverter userConverter;
    private final RightRoleConverter rightRoleConverter;
    private final NavigateConverter navigateConverter;

    @Override
    @Cacheable(value = "User", key = "#root.methodName + '_' + #userName")
    public UserDTO getUserByUserName(String userName) {
        Optional<User> userOptional = userRepository.getUserByUsername(userName);
        User user = userOptional.orElseThrow(() -> new UsernameNotFoundException("用户不存在！" + userName));
        UserDTO userDTO = userConverter.poConvertDTO(user);

        // 是否有进入后台管理的权限 使用usermanager表的isCwgk字段判断
        userDTO.setIsPermitAdmin(user.getIsCwgk() != null && user.getIsCwgk());

        // 获取用户地区账套 优先使用cwgkDistNo cwgkZtId 没有才取distNo ztId
        if (StringUtil.isNotEmpty(user.getCwgkDistNo())) {
            userDTO.setDistNo(user.getCwgkDistNo());
            userDTO.setDistName(user.getCwgkDistName());
            userDTO.setZtId(user.getCwgkZtId());
        }

        // 适配多地区
        if (userDTO.getDistNo() == null) {
            userDTO.setDistNoList(new String[0]);
        } else {
            userDTO.setDistNoList(StringUtil.split(userDTO.getDistNo(), ","));
        }
        if (userDTO.getZtId() == null) {
            userDTO.setZtIdList(new String[0]);
        } else {
            userDTO.setZtIdList(StringUtil.split(userDTO.getZtId(), ","));
        }

        // 权限
        List<RightRole> rightRoles = rightRoleRepository.findAllByPMain(user.getCwgkPmain());
        List<RightRoleDTO> rightRoleDTOS = rightRoleConverter.poConvertDTO(rightRoles);
        for (RightRoleDTO rightRole : rightRoleDTOS) {
            // 获取对应导航navigate的navName 作为权限关键字
            String pno = rightRole.getPno();
            Navigate navigate = navigateRepository.findByNavNoAndVisibleIsTrue(pno);
            if (navigate != null) {
                rightRole.setNavigateKeyword(navigate.getNavName());
            }
        }
        userDTO.setRightRoles(rightRoleDTOS);

        return userDTO;
    }

//    @Override
//    @Cacheable(value = "userPermitAdmin", key = "#root.methodName + '_' + #userName")
//    public boolean isUserPermitEnterAdmin(String userName) {
//        UserDTO user = getUserByUserName(userName);
//        return user.getIsCwgk() != null && user.getIsCwgk();
//    }
}
